<!-- 弹窗用户 -->

<template>
  <div>
    <div v-if="one.members.length">
      <BtnText @click="toOpen">已选择 {{ one.members.length }} 人</BtnText>
      <BtnText v-if="!isShow" title="清除" @click="toClear" />
    </div>
    <BtnPrimary v-else-if="!isShow" title="选择" @click="toOpen" />

    <EasyModal v-model="easyVisible" title="选择小组成员" large @submit="toSubmit" @close="toReset">
      <ChooseUser :obj="obj" bind="user" hasMobile />
    </EasyModal>
  </div>
</template>

<script>
// ============================== 导入依赖 ============================== //

import CORE from '@/CORE'

// ============================== 导出组件 ============================== //

export default {
  /**
   * 名称定义 (实际使用名称)
   */
  name: 'ModalUser',

  /**
   * 混入列表 (配置选项合并)
   */
  mixins: [CORE.mixWeb()],

  /**
   * 状态数据 (函数返回对象)
   */
  data() {
    return {
      easyVisible: false, // 弹窗是否可见
      obj: { user: [] }, // 数据对象
    }
  },

  /**
   * 监听属性 (深度 deep 首次 immediate)
   */
  watch: {
    /**
     * 监听弹窗可见
     */
    easyVisible(visible) {
      visible && this.toReset()
    },
  },

  /**
   * 本地方法 (轻量重要在前)
   */
  methods: {
    /**
     * 弹窗
     */
    toOpen() {
      this.easyVisible = true
    },

    /**
     * 提交
     */
    toSubmit() {
      const { obj, one } = this
      one.members = obj.user.map((item) => ({
        userId: item.id,
        userUaaId: item.uaaUserId,
        userName: item.name,
        userPhone: item.phone,
        deptId: item.organId.join(),
        deptName: item.organ,
      }))
      one.userIds = obj.user.map((item) => item.id).join()
      this.easyVisible = false
    },

    /**
     * 重置
     */
    toReset() {
      const user = this.one.members.map((item) => ({
        id: item.userId,
        uaaUserId: item.userUaaId,
        name: item.userName,
        phone: item.userPhone,
        organId: item.deptId.split(','),
        organ: item.deptName,
      }))
      this.obj = { user }
    },

    /**
     * 清除
     */
    toClear() {
      this.one.members = []
      this.one.userIds = ''
      this.obj = { user: [] }
    },
  },
}
</script>
